<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="gcName">
        <el-input
          v-model="queryParams.gcName"
          placeholder="请输入分类名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gbs:class:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs value="1" @tab-click="handleClickTab">
      <el-tab-pane label="物料分类" name="1" />
      <el-tab-pane label="产品分类" name="0" />
    </el-tabs>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="classList"
      row-key="gcId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="分类名" align="center" prop="gcName" width="200" />
      <el-table-column label="父级分类" align="center" prop="gcFName" width="150" />
      <el-table-column label="备注" align="center" prop="gcDesc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gbs:class:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['gbs:class:add']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gbs:class:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改物品分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="分类名" prop="gcName"
        >
          <el-input v-model="form.gcName" placeholder="请输入分类名"
                    style="width: 205px" />
        </el-form-item>
        <el-form-item label="父级分类" prop="gcFid">
          <el-cascader
            v-model="form.gcFid"
            :options="classOptions"
            :props="classProps"
            :show-all-levels="false"
            style="width: 205px"
          />
        </el-form-item>
        <el-form-item label="备注" prop="gcDesc"
                      style="width: 455px"
        >
          <el-input v-model="form.gcDesc" type="textarea" placeholder="请输入内容" :autosize="{minRows: 3}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addClass, allClass, delClass, getClass, updateClass } from '@/api/yyj/gbs/class'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Class',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 物品分类表格数据
      classList: [],
      // 物品分类树选项
      classOptions: [],
      classProps: {
        label: 'gcName',
        value: 'gcId',
        children: 'children',
        checkStrictly: true,
        emitPath: false
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        gcMaterial: '1',
        gcFid: null,
        gcName: null,
        gcDesc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gcName: [
          { required: true, message: '分类名不能为空', trigger: 'blur' }
        ]
      },
      currentTabName: '1'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 改变tab
    handleClickTab(tab) {
      this.currentTabName = tab.name
      this.queryParams.gcMaterial = tab.name
      this.getList()
    },
    /** 查询物品分类列表 */
    getList() {
      this.loading = true
      allClass(this.queryParams).then(response => {
        this.classList = this.handleTree(response.data, 'gcId', 'gcFid')
        console.log(this.classList)
        this.loading = false
      })
    },
    /** 查询物品分类下拉树结构 */
    async getTreeselect() {
      const res = await allClass({
        gcMaterial: this.currentTabName
      })
      this.classOptions = this.handleTree(res.data, 'gcId', 'gcFid')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        gcId: null,
        gcMaterial: null,
        gcFid: null,
        gcName: null,
        gcDesc: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.getTreeselect()
      if (row != null && row.gcId) {
        this.form.gcMaterial = row.gcMaterial
        this.form.gcFid = row.gcId
      } else {
        this.form.gcMaterial = this.currentTabName
        this.form.gcFid = 0
      }
      this.open = true
      this.title = '添加物品分类'
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.gcFid = row.gcId
      }
      getClass(row.gcId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改物品分类'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          this.$modal.loading('正在提交...')
          if (this.form.gcId != null) {
            await updateClass(this.form)
            this.$modal.msgSuccess('修改成功')
          } else {
            await addClass(this.form)
            this.$modal.msgSuccess('新增成功')
          }
          this.$modal.closeLoading()
          this.open = false
          this.getList()
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除物品分类编号为"' + row.gcId + '"的数据项？').then(function() {
        return delClass(row.gcId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
